<?php
class CommentWidget extends Widget {
	public function render($data) {
		$type	=	t($data ['type']);
		if (isset ( $data ['tpl'] )) {
			
			$data ['list'] = M ( 'Comment' )->where ( " type='" . $type . "' AND appid='{$data['fid']}' AND status = 0 " )->order ( 'id DESC' )->findAll ();
			
			//判断管理员
			$data ['areAdmin'] = (($data ['mid'] == $data ['admin']) || model ( 'UserGroup' )->isAdmin ( $data ['mid'], 'ask' )) ? true : false;
			
			//引用评论功能的辅助数组
			foreach($data['list'] as $v){
				$dataRef[$v['id']]	=	$v;	
			}
			
			//给引用评论赋值
			foreach ( $data ['list'] as &$value ) {
				$value ['child']	=	$dataRef[$value['toId']];
			}
			
			unset($dataRef);

		} else {
			$data ['list'] = M ( 'Comment' )->where ( "type='" . $type . "' AND appid={$data[fid]}" )->findAll ();
		}

		//转换url [杨德升添加]
		foreach($data['list'] as $k => $v) {
			$data['list'][$k]['comment'] = preg_replace('/((?:https?|mailto).*?)(\s|　|&nbsp;|<br|\'|\"|$)/', '<a href="\1" target="_blank">\1</a>\2', $data['list'][$k]['comment']);
		}
		
		$tpl = isset ( $data ['tpl'] ) ? $data ['tpl'] : "Comment";
		if($data['templete'] == true){
			$content = $this->renderTagWidget ( dirname ( __FILE__ ) . "/{$tpl}.html", $data );
		}else{
			$content = $this->renderFile ( dirname ( __FILE__ ) . "/{$tpl}.html", $data );
		}
		return $content;
	}
}
?>